package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    /*用户登录*/
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {

        //判断用户名是否存在
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //select * from user where username  =#{...}
        lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(lambdaQueryWrapper);
        if (one == null) {
            return R.error("用户名不存在");
        }
        //判断密码是否正确
        String password1 = employee.getPassword();
        //采用工具类进行md5加密
        String s = DigestUtils.md5DigestAsHex(password1.getBytes());
        String password2 = one.getPassword();
        if (!s.equals(password2)) {
            return R.error("密码错误");
        }
        //判断status是否被禁用
        Integer status = one.getStatus();
        if (status == 0) {
            return R.error("该员工被禁用");
        }
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    /*退出*/
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return R.success("退出成功");
    }
    /*添加员工*/
    @PostMapping
    public R<String> save(@RequestBody Employee employee) {
        log.info(employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("添加员工成功");
    }


    @GetMapping("page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {
        Page<Employee> page1 = new Page<>(page, pageSize);
//        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
//        wrapper.like(StringUtils.isNotBlank(name), Employee::getName, name);
//        wrapper.orderByDesc(Employee::getCreateTime);
        LambdaQueryWrapper<Employee> wrapper = Wrappers.lambdaQuery(Employee.class)
                .like(StringUtils.isNotBlank(name), Employee::getName, name);
        employeeService.page(page1, wrapper);
        return R.success(page1);
    }

    @PutMapping
    public R<String> update(@RequestBody Employee employee) {
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
